<?php
class ControllerPaymentIps extends Controller {
	
	protected function index() {
		$this->language->load('payment/ips');
		$this->data['text_testmode'] = $this->language->get('text_testmode');		
		$this->data['button_confirm'] = $this->language->get('button_confirm');
		$this->load->model('checkout/order');
		$order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
		$total = $order_info['total'];
		if ($total > 0) {
			$this->data['products'][] = array(
				'name'     => $this->language->get('text_total'),
				'model'    => '',
				'price'    => $total,
				'quantity' => 1,
				'option'   => array(),
				'weight'   => 0
			);	
		} else {
			$this->data['discount_amount_cart'] -= $total;
		}
		$parameters = array();
		$currency_type='';
		if ($order_info['currency_code'] == "CNY") {
			$currency_type = 'RMB';
		} else {
			$this->data['error'] = $this->language->get('error_currency');
		}
		$this->data['pay_url'] =$this->config->get('ips_pay_url');
		$this->data['Mer_code']=$this->config->get('ips_id');
		$this->data['Mer_key']=$this->config->get('ips_key');
		$this->data['Billno']=$this->session->data['order_id'];
		$this->data['Amount'] =number_format($total, 2, '.', '');
		$this->data['Date']=date('Ymd');
		$this->data['Currency_Type']=$currency_type;
		$this->data['Gateway_Type']='01';
		$this->data['Lang']='GB';
		$this->data['Merchanturl']=$this->config->get('ips_merchant_url');
		$this->data['FailUrl']=$this->config->get('ips_fail_url');
		$this->data['ErrorUrl']=$this->config->get('ips_error_url');
		$this->data['Attach']='';
		$this->data['DispAmount']=number_format($total, 2, '.', '');
		$this->data['OrderEncodeType']='5';
		$this->data['RetEncodeType']='17';
		$this->data['Rettype']='1';
		$this->data['ServerUrl']=$this->config->get('ips_server_url');
		$orge = 'billno'.$this->data['Billno'].'currencytype'.$this->data['Currency_Type'].'amount'.$this->data['Amount'].'date'.$this->data['Date'].'orderencodetype'.$this->data['OrderEncodeType'].$this->data['Mer_key'] ;
		$this->data['SignMD5']= md5($orge) ;
		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/payment/ips.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/payment/ips.tpl';
		} else {
			$this->template = 'default/template/payment/ips.tpl';
		}
		$this->data['cancel_return'] = $this->url->link('checkout/checkout', '', 'SSL');
		//recode order
		//$this->model_checkout_order->confirm($this->session->data['order_id'], 1,'',false);
		$this->render();
	}

   /**
	 * 支付成功，返回用户界面
	 * 对应URL:/payment/ips/success
	 * SEO URL:/payment-ips-success
	 */
	public function success() {
		$this->language->load('payment/ips');
		$this->load->model('checkout/order');
		$billno = $_GET['billno'];
		$amount = $_GET['amount'];
		$mydate = $_GET['date'];
		$succ = $_GET['succ'];
		$msg = $_GET['msg'];
		$attach = $_GET['attach'];
		$ipsbillno = $_GET['ipsbillno'];
		$retEncodeType = $_GET['retencodetype'];
		$currency_type = $_GET['Currency_type'];
		$signature = $_GET['signature'];
		$content = 'billno'.$billno.'currencytype'.$currency_type.'amount'.$amount.'date'.$mydate.'succ'.$succ.'ipsbillno'.$ipsbillno.'retencodetype'.$retEncodeType;
		$cert = $this->config->get('ips_key');
		$signature_1ocal = md5($content . $cert);
		$order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
		$order_id=$order_info['order_id'];
		$this->session->data['payment_ips_err_msg']='';
		if ($signature_1ocal == $signature){
			if ($succ == 'Y'){
				$order_total=$order_info['total'];
				if($order_id==$billno&&$order_total==$amount){
					$this->model_checkout_order->confirm($order_id, 2,'',false);
					//$this->model_checkout_order->update($order_id, 2,'',false);
					$this->response->redirect($this->url->link('checkout/success'));
				}else{
					//$msg=",".$order_id."==".$billno."&&".$order_total."==".$amount;
					//$this->session->data['payment_ips_err_msg']=$this->language->get('text_error_unmatch');
					//$this->model_checkout_order->update($order_id, 10,$this->language->get('text_error_unmatch').$msg,false);
					//$this->response->redirect($this->url->link('payment/ips_failure'));
					$this->response->redirect($this->url->link('checkout/success'));
				}
			}else{
				$this->session->data['payment_ips_err_msg']=$this->language->get('text_error_fail');
				$this->model_checkout_order->update($order_id, 10,$this->language->get('text_error_fail'),false);
				$this->response->redirect($this->url->link('payment/ips_failure'));
			}
		}else{
			$this->session->data['payment_ips_err_msg']=$this->language->get('text_error_signature');
			$this->model_checkout_order->update($order_id, 10,$this->language->get('text_error_signature'),false);
			$this->response->redirect($this->url->link('payment/ips_failure'));
		}
	}
	
    /**
	 * 支付成功,调用系统服务进行数据处理
	 * 对应URL:/payment/ips/server
	 * SEO URL:/payment-ips-server
	 */
	public function server() {
		//预留接口
	}
	
	/**
	 * 支付失败
	 * 对应URL:/payment/ips/fail
	 * SEO URL:/payment-ips-fail
	 */
	public function fail() {
		$this->language->load('payment/ips');
		$this->load->model('checkout/order');
		$order_id=$this->session->data['order_id'];
		$this->session->data['payment_ips_err_msg']=$this->language->get('text_error_fail');
		$this->model_checkout_order->update($order_id, 10,$this->language->get('text_error_fail'),false);
		$this->response->redirect($this->url->link('payment/ips_failure'));
	}
	
	/**
	 * 支付错误
	 * 对应URL:/payment/ips/error
	 * SEO URL:/payment-ips-error
	 */
	public function error() {
		$this->language->load('payment/ips');
		$this->load->model('checkout/order');
		$order_id=$this->session->data['order_id'];
		$this->session->data['payment_ips_err_msg']=$this->language->get('text_error_error');
		$this->model_checkout_order->update($order_id, 10,$this->language->get('text_error_error'),false);
		$this->response->redirect($this->url->link('payment/ips_failure'));
	}
}
?>